package com.cqggrw.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.cqggrw.dao.AboutDaoI;
import com.cqggrw.domain.About;
import com.cqggrw.util.DBUtil;
import com.cqggrw.util.Utils;

public class AboutDaoImpl implements AboutDaoI {

	public List<About> getList() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		About about = null;
		List<About> list = new ArrayList<About>();
		
		String sql = "SELECT * FROM tbl_about;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				about = new About();
				about.setId(rs.getInt("id"));
				about.setName(rs.getString("name"));
				about.setContent(rs.getString("content"));
				list.add(about);
			}
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}

	public boolean modify(About about) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "UPDATE tbl_about SET content = ? WHERE id = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, about.getContent());
			ps.setInt(2, about.getId());
			i = ps.executeUpdate();
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

	public About getById(int id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		About about = null;
		
		String sql = "SELECT * FROM tbl_about WHERE id = ?;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				about = new About();
				about.setId(rs.getInt("id"));
				about.setName(rs.getString("name"));
				about.setContent(rs.getString("content"));
			}
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return about;
	}

}
